package com.ycp.erp.login.mobel;

import android.util.Base64;

import com.one.common.common.system.model.response.UserInfoResponse;
import com.one.common.config.CMemoryData;
import com.one.common.config.CPersisData;
import com.one.common.config.MyContact;
import com.one.common.model.http.base.BaseModel;
import com.one.common.model.http.callback.ObserverOnNextListener;
import com.one.common.model.http.callback.ObserverOnResultListener;
import com.one.common.utils.MD5;
import com.one.common.utils.StringUtils;
import com.one.common.view.base.BaseActivity;
import com.ycp.erp.login.mobel.param.CheckCodeParam;
import com.ycp.erp.login.mobel.param.CheckOldPwdParam;
import com.ycp.erp.login.mobel.param.LocationParam;
import com.ycp.erp.login.mobel.param.LoginParam;
import com.ycp.erp.login.mobel.param.ModifyPhoneAtLoginParam;
import com.ycp.erp.login.mobel.param.ModifyPhoneParam;
import com.ycp.erp.login.mobel.param.RegisterParam;
import com.ycp.erp.login.mobel.param.SendCodeParam;
import com.ycp.erp.login.mobel.param.SetPwdParam;
import com.ycp.erp.login.mobel.response.DefaultResponse;

import static com.ycp.erp.login.mobel.LoginApi.CHECK_CODE;
import static com.ycp.erp.login.mobel.LoginApi.CHECK_OLD_PWD;
import static com.ycp.erp.login.mobel.LoginApi.LOGIN;
import static com.ycp.erp.login.mobel.LoginApi.LOGOUT;
import static com.ycp.erp.login.mobel.LoginApi.MODIFYPHONE;
import static com.ycp.erp.login.mobel.LoginApi.MODIFYPHONEATLOGIN;
import static com.ycp.erp.login.mobel.LoginApi.REGISTER;
import static com.ycp.erp.login.mobel.LoginApi.SEND_CODE;
import static com.ycp.erp.login.mobel.LoginApi.SET_PWD;


/**
 * Author: zhulunjun
 * Email: zhulunjun@qq.com
 * CreateDate: 2018/11/15 19:21
 * Description：登录模块
 **/
public class LoginModel extends BaseModel<LoginApi> {
    public LoginModel(BaseActivity mActivity) {
        super(LoginApi.class, mActivity);
    }


    /**
     * 发送验证码
     */
    public void sendCode(String mobile, String note_type, ObserverOnNextListener<DefaultResponse> listener) {
        String key = MD5.Md5("mobile=" + mobile + "&" + "note_type=" + note_type +  "&" + "key=" + MyContact.REGISTER_KEY);
        String str = "";
        try {
            str = Base64.encodeToString(key.getBytes(), Base64.NO_WRAP);
        } catch (Exception ex) {
        }

        mParam = getParams(SEND_CODE, new SendCodeParam(mobile, note_type, str));
        handleOnNextObserver(mApiService.sendCode(mParam), listener);

    }

    /**
     * 登录
     */
    public void login(String mobile, String word, String login_type, LocationParam locationParam,
                      ObserverOnNextListener<UserInfoResponse> listener) {
        LoginParam param = new LoginParam(mobile, login_type, word);
        setLocationInfo(param, locationParam);
        mParam = getParams(LOGIN, param);
        handleOnNextObserver(mApiService.login(mParam), listener);

    }

    /**
     * 设置定位信息和激光id 机型
     */
    private void setLocationInfo(LocationParam param, LocationParam locationInfo) {
        // 设置激光id
        if (StringUtils.isBlank(CPersisData.getJpushId())) {
            CPersisData.setJpushId(mActivity);
        }
        param.setMsg_push_token(CPersisData.getJpushId());
        param.setLogin_mobile_type(android.os.Build.MODEL);
        // 设置定位信息
        if (locationInfo != null) {
            param.setLogin_address(locationInfo.getLogin_address());
            param.setLogin_city_id(locationInfo.getLogin_city_id());
            param.setLogin_latitude(locationInfo.getLogin_latitude());
            param.setLogin_longitude(locationInfo.getLogin_longitude());
        }
    }

    /**
     * 注册
     */
    public void register(String mobile, String pwd, String code, String inviteCode, LocationParam locationParam, String truckerType,
                         ObserverOnResultListener<UserInfoResponse> listener) {
        RegisterParam param = new RegisterParam(mobile, pwd, code, inviteCode);

        if (CMemoryData.isCar()){
            param.setTruckerType(truckerType);
        }

        setLocationInfo(param, locationParam);
        mParam = getParams(REGISTER, param);
        handleOnResultObserver(mApiService.register(mParam), listener);

    }

    /**
     * 校验验证码(找回密码用)
     */
    public void checkCode(String mobile, String code, ObserverOnResultListener<DefaultResponse> listener) {
        mParam = getParams(CHECK_CODE, new CheckCodeParam(mobile, code));
        handleOnResultObserver(mApiService.checkCode(mParam), listener);

    }

    /**
     * 修改密码/设置密码
     */
    public void setPwd(String mobile, String pwd, ObserverOnResultListener<DefaultResponse> listener) {
        mParam = getParams(SET_PWD, new SetPwdParam(mobile, pwd));
        handleOnResultObserver(mApiService.setPwd(mParam), listener);

    }

    /**
     * 验证旧密码
     */
    public void checkOldPwd(String pwd, ObserverOnResultListener<DefaultResponse> listener) {
        mParam = getParams(CHECK_OLD_PWD, new CheckOldPwdParam(pwd));
        handleOnResultObserver(mApiService.checkOldPwd(mParam), listener);

    }

    /**
     * 退出登录
     */
    public void logout(ObserverOnResultListener<DefaultResponse> listener) {
        mParam = getParams(LOGOUT);
        handleOnResultObserver(mApiService.logout(mParam), listener);

    }


    public void modifyPhone(String phoneNew, String code, ObserverOnResultListener<DefaultResponse> listener) {
        mParam = getParams(MODIFYPHONE, new ModifyPhoneParam(phoneNew, code));
        handleOnResultObserver(mApiService.modifyPhone(mParam), listener);

    }

    public void modifyPhoneAtLogin(String phoneOld, String phoneNew, String code,
                                   String key_headurl, String key_idcardurl, ObserverOnResultListener<DefaultResponse> listener) {
        mParam = getParams(MODIFYPHONEATLOGIN, new ModifyPhoneAtLoginParam(phoneOld, phoneNew, code, key_headurl, key_idcardurl));
        handleOnResultObserver(mApiService.modifyPhoneAtLogin(mParam), listener);

    }
}
